package classloader;

import entity.Student;
import org.junit.Test;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.sql.SQLOutput;
import java.util.Arrays;
import java.util.stream.Collector;

public class ClassLoaderDemo {
    @Test
    public void test1() {
        ClassLoader s = ClassLoader.getSystemClassLoader();
        System.out.println(s);
        ClassLoader parent = s.getParent();
        System.out.println(parent);
    }

    // @Test
    // public void test2() throws ClassNotFoundException {
    //     Class<Student> s1 = Student.class;
    //     System.out.println(s1);

    //     Class<Student> s2 = Student.class;
    //     System.out.println(s2);
    //     System.out.println(s1 == s2);
    //     System.out.println("------");

    //     Student s = new Student();
    //     Class<? extends Student> s3 = s.getClass();
    //     System.out.println(s2==s3);

    //     //使用Class类中的静态方法forName(String className)
    //     Class<?> s4 = Class.forName("entity.Student");
    //     System.out.println(s2==s4);
    // }

    @Test
    public void test3() throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
        Class<?> c = Class.forName("entity.Student");
        Constructor<?>[] cons = c.getDeclaredConstructors();
        for(Constructor con : cons){
            System.out.println(con);
        }
        System.out.println("-----------");
            Constructor<?> constructor = c.getConstructor(Integer.class, String.class, String.class);
            Object o = constructor.newInstance(1,"jhl","111111");
            System.out.println(o);
    }
    @Test
    public void test4() throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
        Class<?> c = Class.forName("entity.Student");

        Constructor<?> declaredConstructor = c.getDeclaredConstructor(String.class);
        declaredConstructor.setAccessible(true);
        Object o = declaredConstructor.newInstance("222");
        System.out.println(o);
    }

    // 获取构造方法
    @Test
    public void test5() throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
        Class<?> clazz = Class.forName("entity.Student");
        Constructor<?>[] declaredConstructors = clazz.getDeclaredConstructors();
        for(Constructor c : declaredConstructors){
            String simpleName = clazz.getSimpleName(); // 获取构造方法名
            System.out.println(simpleName);
            Class[] cs2 = c.getParameterTypes();
            System.out.println(Arrays.toString(cs2));
        }
    }

    // 获取成员方法
    @Test
    public void test6() throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
        Class<?> Clazz = Class.forName("entity.Student");
        Method[] method = Clazz.getMethods();
        for(Method m : method){
            String name = m.getName(); // 获取成员方法名
            System.out.println(name);

            Class<?>[] parameterTypes = m.getParameterTypes();
            System.out.println(Arrays.toString(parameterTypes));
        }

    }

    // 获取成员变量
    @Test
    public void test7() throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
        Class<?> Clazz = Class.forName("entity.Student");
        Field[] declaredFields = Clazz.getDeclaredFields();
        for(Field f : declaredFields){
            String name = f.getName();
            Class<?> type = f.getType();
            String simpleName = type.getSimpleName();
            System.out.println(f);
            System.out.println(name);
            System.out.println(type);
            System.out.println(simpleName);
            System.out.println("----------------------------");
        }

    }

    // 创建对象
    @Test
    public void test8() throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
        Class<?> Clazz = Class.forName("entity.Student");
        Object o = Clazz.newInstance();
        System.out.println(o);

        Constructor<?> constructor = Clazz.getConstructor(String.class);
        Object jack = constructor.newInstance("jack");
        System.out.println(jack);
    }



}
